﻿using LbangCMS.EFModels;
using LbangCMS.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Controllers
{
    public class ProductController : Controller
    {
        // GET: Product
        public ActionResult Index(int categoryId = 0, int pageIndex=1,int pageSize=6)
        {
            //获取分类
            //获取排序最小者
            //根据最小者去获取商品信息
            //分页
            var categories = GetAllProductCategoryList();
            if(categoryId==0&&categories.Any())
            {
                categoryId = categories.OrderBy(o => o.SortIndex).FirstOrDefault().ID;
            }
            int totalPage;
            var products = GetProductList(categoryId, pageIndex, pageSize,out totalPage);
            var model = new IndexProductListModel();
            model.Categories = new List<ProductCategoryModel>();
            if(categories!=null&&categories.Any())
            {
                foreach (var item in categories)
                {
                    model.Categories.Add(new ProductCategoryModel() {
                        ID=item.ID,
                        Name=item.Name,
                        SortIndex=item.SortIndex
                    });
                }
            }
            model.CurrentCategoryId = categoryId;
            var category = GetProductCategory(categoryId);
            model.CurrentCategoryName = category.Name;
            model.Products = new List<ProductModel>();
            if (products!=null&&products.Any())
            {
                foreach (var item in products)
                {
                    model.Products.Add(new ProductModel() { CategoryId=item.CategoryId,ID=item.ID,Description=item.Description,ImageSrc=item.ImageSrc,IsPublish=item.IsPublish,IsRecommend=item.IsRecommend,Name=item.Name });
                }
            }

            return View(model);
        }

        public ActionResult Detail(int id)
        {
            var product = GetProduct(id);
            if(product!=null)
            {
                var recommendProducts = GetRecommendProduct(4);
                var model = new ProductDetailModel();
                model.Categories = new List<ProductCategoryModel>();
                var categories = GetAllProductCategoryList();
                if (categories != null && categories.Any())
                {
                    foreach (var item in categories)
                    {
                        model.Categories.Add(new ProductCategoryModel()
                        {
                            ID = item.ID,
                            Name = item.Name,
                            SortIndex = item.SortIndex
                        });
                    }
                }
                //获取推荐商品
                //获取商品分类
                //设置当前分类Id
                var p = new ProductModel();
                p.ID = product.ID;
                p.ImageSrc = product.ImageSrc;
                p.Name = product.Name;
                p.Description = product.Description;
                p.CategoryId = product.CategoryId;
                p.IsPublish = product.IsPublish;
                p.IsRecommend = product.IsRecommend;
               
                model.Product = p;
                model.RecommendProducts = new List<ProductModel>();
                var catetgory = GetProductCategory(p.CategoryId);
                model.CategoryName = catetgory.Name;
                model.Properties = new List<ProductPropertyModel>();
                var props = GetProductProperty(product.ID);
                if(props!=null&&props.Any())
                {
                    foreach (var item in props)
                    {
                        model.Properties.Add(new ProductPropertyModel { PropertyName=item.PropertyName,PropertyValue=item.PropertyValue,SortIndex=item.SortIndex });
                    }
                }
                if (recommendProducts!=null&&recommendProducts.Any())
                {
                    foreach (var item in recommendProducts)
                    {
                        model.RecommendProducts.Add(new ProductModel() { ID=item.ID,CategoryId=item.CategoryId,Description=item.Description,ImageSrc=item.ImageSrc,IsPublish=item.IsPublish,IsRecommend=item.IsRecommend,Name=item.Name });
                    }
                }
                model.CurrentCategoryId = product.CategoryId;
                ViewBag.Title = product.Name;
                return View(model);
            }
            else
            {
                return Content("404");
            }
        }

        #region private
        private Product GetProduct(int id)
        {
            using (var db = new Database())
            {
                return db.Products.Where(o => o.ID == id).FirstOrDefault();
            }
        }

        private List<ProductCategory> GetAllProductCategoryList()
        {
            using (var db = new Database())
            {
                return db.ProductCategories.OrderBy(o => o.SortIndex).ToList();
            }
        }

        private ProductCategory GetProductCategory(int categoryId)
        {
            using (var db = new Database())
            {
                return db.ProductCategories.Where(o => o.ID == categoryId).FirstOrDefault();
            }
        }

        private List<Product> GetProductList(int categoryId, int pageIndex, int pageSize,out int totalPage)
        {
            using (var db = new Database())
            {
                var count = db.Products.Where(o => o.CategoryId == categoryId).Count();
                totalPage = (int)Math.Ceiling(count / (pageSize * 1M));
                return db.Products.Where(o => o.CategoryId == categoryId).OrderByDescending(o => o.ID).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); ;
            }
        }

        private List<Product> GetRecommendProduct(int quantity)
        {
            using (var db = new Database())
            {
                return db.Products.Where(o => o.IsRecommend == true).OrderByDescending(o => o.ID).Take(quantity).ToList();
            }
        }

        private List<ProductProperty> GetProductProperty(int productId)
        {
            using (var db = new Database())
            {
                return db.ProductProperties.Where(o => o.ProductId == productId).OrderBy(o => o.SortIndex).ToList();
            }
        }
        #endregion
    }
}